package cn.itcast.mq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class CommonConfig implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //RabbitTemplate 对象是用来发消息的
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        //异步回调 发消息是成功还是失败
        rabbitTemplate.setReturnCallback((message, code, reason, exchange, routingKey) -> {
            log.info("到达交换机,队列失败进行结果确认: code:{},原因:{},交换机:{},路由key:{},消息:{}",code,reason,exchange,routingKey,message);
        });
    }
}
